///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef __BUOLA_APP_CTIMER_H__
#define __BUOLA_APP_CTIMER_H__

#include <buola/buola.h>
#include <buola/utility/usignal.h>
#include <buola/datetime/clocks.h>

namespace buola {

///\ingroup signals datetime
///@{

///////////////////////////////////////////////////////////////////////////

/// signals after a certain interval has elapsed

/// Objects of this class emit a signal after the time interval expires.
/// When the timer expires, it is reset again to the same value, so it
/// will signal again after another time interval, unless the FLAG_ONCE
/// is set or the timer is manually disabled
///
///////////////////////////////////////////////////////////////////////////

class CTimer : public UUncopiable
{
public:
    //! flags for the timer
    enum EFlags
    {
        FLAG_NONE=0,
        FLAG_ONCE=(1<<0),		//!< disable after signalling
        FLAG_SIGNALALL=(1<<1)  	//!< makes sure a signal is delivered after every interval,
                                //!< even if more time elapsed
    };

    DECLARE_FRIEND_ENUM_BITOPS(EFlags)

//! \name constructor and destructor
//@{
    //! constructs the timer and associates it to the main event loop

    //! \param pInterval time interval between signals
    //! \param pActive whether it should start activated
    //! \param pFlags initial flags
    CTimer(const chrono::microseconds &pInterval=chrono::seconds(1),bool pActive=false,EFlags pFlags=FLAG_NONE);

    //! constructs the timer and associates it to a given event loop

    //! \param pEventLoop event loop from which timer events will be dispatched
    //! \param pInterval time interval between signals
    //! \param pActive whether it should start activated
    //! \param pFlags initial flags
    CTimer(CEventLoop &pEventLoop,const chrono::microseconds &pInterval=chrono::seconds(1),
           bool pActive=false,EFlags pFlags=FLAG_NONE);

    ~CTimer();
//@}

    //! sets the interval and optionally enables the timer
    template<typename tRep,typename tPeriod>
    void SetInterval(const chrono::duration<tRep,tPeriod> &pInterval,bool pEnable=false)
    {
        mInterval=chrono::duration_cast<chrono::microseconds>(pInterval);

        if(mActive||pEnable) Enable();
    }

#if BUOLA_PLATFORM_LINUX
    //! sets the next trigger time and enables the timer
    void SetAbsTime(const CCalendarTime &pTime);
#endif //BUOLA_PLATFORM_LINUX

    //! sets the next trigger time and enables the timer
    void SetAbsTime(const CMonotonicTime &pTime);

    //! modify the timer flags
    void SetFlags(EFlags pFlags);

    //! enables the timer
    void Enable();
    //! disables the timer
    void Disable();
    
    void Toggle()
    {
        if(mActive)
            Disable();
        else
            Enable();
    }
    
    bool Active()   {   return mActive; }

public:
    //! will be dispatched after the interval
    USignal<void()> sTimer;

private:
    //! time of next activation
    CMonotonicTime mNext;
    //! interval
    chrono::microseconds mInterval;
    //! true if it is active
    bool mActive;
    //! timer flags
    EFlags mFlags;

    //! deletion guard
    bool *mDeleted;

    //! event loop associated to this timer
    CEventLoop &mEventLoop;

    friend class CEventLoop;
};

///@}

/*namespace buola*/ }

#endif
